import scrapy
import json
from ..items import UserItem


class ContactSpider(scrapy.Spider):
    name = "contact"
    headers = {
        'Accept':
        'application/json, text/plain, */*',
        'Accept-Language':
        'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Referer':
        'https://xueqiu.com/center/',
        'Host':
        'xueqiu.com',
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36',
        'Cookie':
        'aliyungf_tc=AQAAAO7pDBpyzAEAqrjkdH7pYfaLIte7; Hm_lvt_1db88642e346389874251b5a1eded6e3=1549071616; _ga=GA1.2.1064288005.1549071616; _gid=GA1.2.1863692746.1549071616; device_id=53187d8834958317958b45c9b2c8e078; remember=1; remember.sig=K4F3faYzmVuqC0iXIERCQf55g2Y; xq_a_token=7a8bb714254d0cb8a95061cc6dd23648e94f81c7; xq_a_token.sig=zGhPLKhEwASCORo7vntl62xzVDw; xqat=7a8bb714254d0cb8a95061cc6dd23648e94f81c7; xqat.sig=eE0mDKMGZhECtqyWRw56Zh0RqpI; xq_r_token=d8ae582a2c0313c70ded8541cb19dbadade3a680; xq_r_token.sig=_32XuBluFO8hC0jDGQqrT4Q2E60; xq_is_login=1; xq_is_login.sig=J3LxgPVPUzbBg3Kee_PquUfih7Q; u=6070068341; u.sig=-vC9yji8DEwix-eqx92kRwVyCSw; snbim_minify=true; s=dp18xrs0nk; bid=d6e01127c7db51382d28451b246c38a2_jrmth7l1; __utma=1.1064288005.1549071616.1549072009.1549072009.1; __utmc=1; __utmz=1.1549072009.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _gat_gtag_UA_16079156_4=1; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1549079457'
    }
    url_base = "https://xueqiu.com/friendships/groups/members.json?uid=%d&page=%d&gid=0"

    def start_requests(self):
        uid = 8160302897
        yield scrapy.Request(
            url=self.url_base % (uid, 1),
            headers=self.headers,
            callback=self.request_group_user,
            meta={'current_uid': uid})

    def request_group_user(self, response):
        uid = response.meta['current_uid']
        jsonresponse = json.loads(response.body_as_unicode())
        maxPage = jsonresponse['maxPage']
        page = jsonresponse['page']

        for ujson in jsonresponse['users']:
            item = UserItem()
            item['id'] = ujson['id']
            item['screen_name'] = ujson['screen_name']
            item['followers_count'] = ujson['followers_count']
            item['friends_count'] = ujson['friends_count']
            item['cube_count'] = ujson['cube_count']
            yield item

        if page <= maxPage:
            yield scrapy.Request(
                self.url_base % (uid, page + 1),
                headers=self.headers,
                callback=self.request_group_user,
                meta={'current_uid': uid})
